/*
 * Created on 2007-8-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.tongji.quickCRUDQ.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;

import com.tongji.quickCRUDQ.define.Table;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TableDao extends JdbcDaoSupport {
	private TableQuery tableQuery;
	private TableQuery tableAliasQuery;


	private static final String TALBE_SQL = 
		" SELECT     ID, TABLENAME, NOTE, TITLE, DEFAULTWHERE, \n"+
	    " DEFAULTORDERBY, APPENDMAIN, ROWSOFPAGE, QUERYTYPE, BSUM, PARENTTABLE, \n"+
        " REFERENCECOLUMN \n"+
        " FROM  TABLE_DEF  \n"+
	    " where tablename=?\n";
	
	private static final String TALBE_ALIAS_SQL = 
		" SELECT     ID, TABLENAME, NOTE, TITLE, DEFAULTWHERE, \n"+
		" DEFAULTORDERBY, APPENDMAIN, ROWSOFPAGE, QUERYTYPE, BSUM, PARENTTABLE, \n"+
		" REFERENCECOLUMN \n"+
		" FROM  TABLE_DEF_ALIAS  \n"+
		" where tablename=? and tablealias=? \n";

	protected void initDao() throws Exception {
		// TODO Auto-generated method stub
		super.initDao();
		tableQuery = new TableQuery(this.getDataSource());
		tableAliasQuery = new TableQuery(this.getDataSource(),TALBE_ALIAS_SQL);
	}

	public TableDao() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Table getTable(String tableName) {
		List list = this.tableQuery.execute(tableName);
		if (list.size() == 0) {
			return null;
		} else {
			return (Table) list.get(0);
		}
	}
	public Table getTable(String tableName, String tableAlias) {
		if (tableAlias==null||"".equals(tableAlias)) {
			return getTable(tableName);
		} else {
			return getTableAlias(tableName,tableAlias);
		}
	}	
	
	private Table getTableAlias(String tableName, String tableAlias){
		List list = this.tableAliasQuery.execute(new String[]{tableName,tableAlias});
		if (list.size() == 0) {
			return getTable(tableName);
		} else {
			return (Table) list.get(0);
		}		
	}	
//	public Table getParentTable(String tableName){
//		Table table=this.getTable(tableName);
//		if(table==null) return null;
//		String parentTableName=table.getParentTable();
//		if(parentTableName==null) return null;
//		return this.getTable(parentTableName);
//	}

	/**
	 * @author Administrator
	 *
	 * TODO To change the template for this generated type comment go to
	 * Window - Preferences - Java - Code Style - Code Templates
	 */
	class TableQuery extends MappingSqlQuery {

		public TableQuery(DataSource datasource) {
			super(datasource, TALBE_SQL);
			declareParameter(new SqlParameter(Types.VARCHAR));
		}
		public TableQuery(DataSource datasource,String sql) {
			super(datasource, sql);
			declareParameter(new SqlParameter(Types.VARCHAR));
			declareParameter(new SqlParameter(Types.VARCHAR));
		}
		/* (non-Javadoc)
		 * @see org.springframework.jdbc.object.MappingSqlQuery#mapRow(java.sql.ResultSet, int)
		 */
		protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Table table = new Table();
			table.setId(rs.getString("ID"));
			table.setTablename(rs.getString("TABLENAME"));
			table.setNote(rs.getString("NOTE"));
			table.setTitle(rs.getString("TITLE"));
			table.setDefaultwhere(rs.getString("DEFAULTWHERE"));
			table.setDefaultorderby(rs.getString("DEFAULTORDERBY"));
			table.setAppendmain(rs.getString("APPENDMAIN"));
			table.setQueryType(rs.getString("QUERYTYPE"));
			int rowsOfPage=rs.getInt("rowsOfPage");
			table.setRowsOfPage(rowsOfPage==0?20:rowsOfPage);
			table.setBSum(rs.getString("BSUM"));
			table.setParentTable(rs.getString("PARENTTABLE"));
			table.setReferenceColumn(rs.getString("REFERENCECOLUMN"));
			return table;
		}
	}

}
